package pers.vic.xqd.protocol.http;

import org.apache.commons.io.IOUtils;
import pers.vic.xqd.framerwork.Invocation;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * 描述:
 * @author Vic.xu
 * @date 2022-02-08 11:32
 */
public class HttpClient {

    public String send(String hostname, Integer port, Invocation invocation){


        try {
            URL url = new URL("http",hostname, port, "/");
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);

            OutputStream outputStream = httpURLConnection.getOutputStream();

            ObjectOutputStream oos = new ObjectOutputStream(outputStream);
            oos.writeObject(invocation);
            oos.flush();
            oos.close();

            InputStream inputStream = httpURLConnection.getInputStream();
            String result = IOUtils.toString(inputStream);
            return result;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }


        return null;
    }

}
